import pymysql


class DB:
    def __init__(self):
        self.conn = pymysql.connect(
            host='127.0.0.1',
            port=3306,
            user='root',
            password='123',
            db='zuzhi',
            charset='utf8',
            cursorclass=pymysql.cursors.DictCursor
        )
        self.crous = self.conn.cursor()

    def update(self, sql):
        self.crous.execute(sql)
        self.conn.commit()

    def find(self, sql):
        self.conn.ping(reconnect=True)
        self.crous.execute(sql)
        res = self.crous.fetchone()
        return res

    def findall(self, sql):
        self.conn.ping(reconnect=True)
        self.crous.execute(sql)
        res = self.crous.fetchall()
        return res

    def close(self):
        self.crous.close()
        self.conn.close()

    def add(self,sql):
        self.crous.execute(sql)

    def commit(self):
        self.conn.commit()

    def rollback(self):
        self.conn.rollback()


db1 = DB()
